package com.tj.collection.db.dao.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.tj.collection.db.dao.ClAllocationDetailDao;
import com.tj.collection.db.entity.ClAllocationDetailEntity;
import com.tj.collection.db.entity.business.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2019-09-03
 * Time: 10:18
 */
@Repository
public class ClAllocationDetailDaoImpl extends AbstractBaseMapper<ClAllocationDetailEntity> implements ClAllocationDetailDao {

    private static final Log LOGGER = LogFactory.getLog(com.tj.collection.db.dao.impl.ClAllocationDetailDaoImpl.class);

    @Override
    public ClAllocationDetailEntity getCurrentAllocationByCollectionNo(String collectionNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        return getSqlSession().selectOne(this.getStatement("getCurrentAllocationByCollectionNo"), map);
    }

    @Override
    public List<ClAllocationDetailEntity> getListByCollectionNo(String collectionNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        return getSqlSession().selectList(this.getStatement("getListByCollectionNo"), map);
    }

    @Override
    public ClAllocationDetailEntity getLatestRecordByCollectionNoAndRepaymentDate(String collectionNo, Date repaymentDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        map.put("repaymentDate", repaymentDate);
        return getSqlSession().selectOne(this.getStatement("getLatestRecordByCollectionNoAndRepaymentDate"), map);
    }

    @Override
    public ClAllocationDetailEntity getLatestByCollectionNoAndLessThanDate(String collectionNo, Date repaymentDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        map.put("repaymentDate", repaymentDate);
        return getSqlSession().selectOne(this.getStatement("getLatestByCollectionNoAndLessThanDate"), map);
    }

    /**
     * 获取未完结的订单
     *
     * @param batchNo
     * @return
     */
    @Override
    public List<ClAllocationDetailEntity> getListByBatchNo(String batchNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("batchNo", batchNo);
        return getSqlSession().selectList(this.getStatement("getListByBatchNo"), map);
    }

    @Override
    public List<ClAllocationDetailEntity> getListByBatchNosAndPage(List<String> batchNos, Integer startIndex, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("batchNos", batchNos);
        map.put("startIndex", startIndex);
        map.put("pageSize", pageSize);
        return getSqlSession().selectList(this.getStatement("getListByBatchNosAndPage"), map);
    }

    @Override
    public Integer countByBatchNosAndPage(List<String> batchNos) {
        Map<String, Object> map = new HashMap<>();
        map.put("batchNos", batchNos);
        return getSqlSession().selectOne(this.getStatement("countByBatchNosAndPage"), map);
    }

    @Override
    public List<ClAllocationInfoEntity> selectAllocationFullInfoList(Map<String, Object> params, Page page) {
        if (page != null) {
            page.setTotalCount(this.queryTotalAllocationFullInfo(params));
            params.put("page", page);
        }
        return getSqlSession().selectList(this.getStatement("selectAllocationFullInfoList"), params);
    }

    @Override
    public List<ClAllocationInfoEntity> selectAllocationFullInfoList(Map<String, Object> params, Page page, Integer totalCount) {
        if (page != null) {
            if (totalCount != null) {
                page.setTotalCount(totalCount);
                LOGGER.info("selectAllocationFullInfoList,缓存,params:{},totalCount:{}", JSON.toJSONString(params), totalCount);
            } else {
                page.setTotalCount(this.queryTotalAllocationFullInfo(params));
                LOGGER.info("selectAllocationFullInfoList,查库,params:{},totalCount:{}", JSON.toJSONString(params), page.getTotalCount());
            }
            params.put("page", page);
        }
        return getSqlSession().selectList(this.getStatement("selectAllocationFullInfoList"), params);
    }

    private Integer queryTotalAllocationFullInfo(Map<String, Object> params) {
        return getSqlSession().selectOne(this.getStatement("queryTotalAllocationFullInfo"), params);
    }

    @Override
    public List<ClOrderAllocationEntity> selectOrderAllocationList(Map<String, Object> params, Page page) {
        if (page != null) {
            page.setTotalCount(this.queryTotalOrderAllocation(params));
            params.put("page", page);
        }
        return getSqlSession().selectList(this.getStatement("selectOrderAllocationList"), params);
    }

    private Integer queryTotalOrderAllocation(Map<String, Object> params) {
        return getSqlSession().selectOne(this.getStatement("queryTotalOrderAllocation"), params);
    }

    @Override
    public List<ClOrderAllocationEntity> selectCollectionAllocationHistoryList(String userCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("userCode", userCode);
        return getSqlSession().selectList(this.getStatement("selectCollectionAllocationHistoryList"), params);
    }

    public List<UniqueStatsVo> getAgentUniqueStats(String stageCode, Date startDate, Date endDate, String merchantCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("stageCode", stageCode);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("merchantCode", merchantCode);
        return getSqlSession().selectList(this.getStatement("getAgentUniqueStats"), map);
    }

    @Override
    public List<UniqueStatsVo> getAgentAllStats(Date startDate, Date endDate, Integer adminId, String merchantCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("adminId", adminId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("merchantCode", merchantCode);
        return getSqlSession().selectList(this.getStatement("getAgentAllStats"), map);
    }

    @Override
    public List<UniqueStatsVo> getAgentAllStatsGroup(String stageCode, Date startDate, Date endDate, String merchantCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("stageCode", stageCode);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("merchantCode", merchantCode);
        return getSqlSession().selectList(this.getStatement("getAgentAllStatsGroup"), map);
    }

    @Override
    public Integer updateStatusByBatchNoAndStatus(String batchNo, List<Integer> sourceStatus, Integer targetStatus) {
        if (StringUtils.isEmpty(batchNo) || CollectionUtils.isEmpty(sourceStatus) || targetStatus == null) {
            return 0;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("batchNo", batchNo);
        map.put("sourceStatus", sourceStatus);
        map.put("targetStatus", targetStatus);
        return getSqlSession().update(this.getStatement("updateStatusByBatchNoAndStatus"), map);
    }

    @Override
    public void updateStatusByAllocationNosAndStatus(List<String> allocationNos, List<Integer> sourceStatus, Integer targetStatus) {
        Map<String, Object> map = new HashMap<>();
        map.put("allocationNos", allocationNos);
        map.put("sourceStatus", sourceStatus);
        map.put("targetStatus", targetStatus);
        getSqlSession().selectList(this.getStatement("updateStatusByAllocationNosAndStatus"), map);
    }

    @Override
    public ClCountInfoEntity selectAgentWorkStats(Integer adminId, Date startDate, Date endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("adminId", adminId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return getSqlSession().selectOne(this.getStatement("selectAgentWorkStats"), map);
    }

    /**
     * 把当前催收单的分单 id > 指定值的 status=0 的置为无效4
     *
     * @param collectionNo
     * @param id
     * @return
     */
    @Override
    public int updateAllocationInvalid(String collectionNo, long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        map.put("id", id);
        return getSqlSession().update(this.getStatement("updateAllocationInvalid"), map);
    }

    /**
     * 查询代理人 当日新增分单数
     *
     * @param merchantCode
     * @param date
     * @param overdueDays
     * @return
     */
    @Override
    public List<ClDailyOrderCountEntity> selectAgentDailyOrderCountList(String merchantCode, Date date, Integer overdueDays) {
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        map.put("date", date);
        map.put("overdueDays", overdueDays);
        return getSqlSession().selectList(this.getStatement("selectAgentDailyOrderCountList"), map);
    }
}
